package com.sn.pw.controller;

import com.sn.pw.exception.ServiceException;
import com.sn.pw.page.PageReq;
import com.sn.pw.result.Result;
import com.sn.pw.service.EvaluationService;
import com.sn.pw.vo.createreq.EvaluationCreateReq;
import com.sn.pw.vo.req.EvaluationListReq;
import com.sn.pw.vo.req.IdReq;
import com.sn.pw.vo.resp.EvaluationResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

import static com.sn.pw.code.impl.BaseErrorCode.SERVICE_ERROR;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(value = "评价信息相关-Api",tags = "评价信息相关-Api")
@RestController
@RequestMapping("/evaluation")
public class EvaluationController {

    private final EvaluationService service;

    @PostMapping("/getEvaluationListByUserId")
    @ApiOperation(value = "根据用户id获取评论信息")
    public Result<List<EvaluationResp>> getEvaluationListByUserId(@Valid @RequestBody IdReq idReq) {
        return Result.success(service.getEvaluationListByUserId(idReq));
    }

    @PostMapping("/getEvaluationListByCompanyId")
    @ApiOperation(value = "根据物流公司id获取评论信息")
    public Result<List<EvaluationResp>> getEvaluationListByCompanyId(@Valid @RequestBody IdReq idReq) {
        return Result.success(service.getEvaluationListByCompanyId(idReq));
    }

    @PostMapping("/create")
    @ApiOperation(value = "新增")
    public Result createOne(@Valid @RequestBody EvaluationCreateReq param){
        if (service.create(param))
            return Result.success("新增成功");
        else
            throw new ServiceException(SERVICE_ERROR);
    }
    @PostMapping("/getEvaluationList")
    @ApiOperation(value = "评价管理")
    public Result<List<EvaluationResp>> getEvaluationList(@Valid @RequestBody EvaluationListReq evaluationListReq){
        return Result.success(service.getEvaluationList(evaluationListReq));
    }
}
